package com.ias.agdis_r.util.date;

import android.content.Context;
import android.text.format.DateFormat;

import com.ias.agdis_r.R;
import com.ias.agdis_r.manager.DataBaseManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 
 * @author qibo.yu
 * @version 2015年8月21日 下午4:21:12
 **/
public class PrettyDataFormaterUtil {

	private static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss",Locale.CHINA);

	public static final long UNIT_SECOND = 1000L;
	public static final long UNIT_MINUTE = UNIT_SECOND * 60L;
	public static final long UNIT_HOUR = UNIT_MINUTE * 60L;
	public static final long UNIT_DAY = UNIT_HOUR * 24L;
	public static final long UNIT_WEEK = UNIT_DAY * 7L;
	private Context context = DataBaseManager.getInstance().getContext();

	public static String formateDate(final Date date, final Context context) {
//		Calendar calendar = Calendar.getInstance();
//		calendar.setTime(date);
//		return formateDate(calendar, context);
		String second;
		if (date.getSeconds()<10){
			second = "0" + date.getSeconds();
		}else{
			second = String.valueOf(date.getSeconds());
		}
		return DateFormat.getDateFormat(context.getApplicationContext()).format(date)+" "+DateFormat.getTimeFormat(context.getApplicationContext()).format(date)+":"+second;
}

	public static String formateDate(final Calendar date, final Context context) {
		final Calendar now = Calendar.getInstance();

		final long difference = now.getTimeInMillis() - date.getTimeInMillis();
		if (now.after(date)) {
			if (isToday(date)) {
				if (isJustNow(difference)) {
					return context.getString(R.string.justNow);
				} else if (isInTenMinutes(difference)) {
					return difference / UNIT_MINUTE + context.getString(R.string.minAgo);
				} else {
					return getPrettyDayString(date, context);
				}
			} else if (isInTwoDays(date)) {
				String prefix = isYesterday(date) ? context.getString(R.string.yesterday) : context.getString(R.string.beforeYes);
				return prefix + getPrettyDayString(date, context);
			} else if (now.get(Calendar.YEAR) == date.get(Calendar.YEAR)) {
				StringBuilder builder = new StringBuilder();
				builder.append(date.get(Calendar.MONTH) + 1).append(context.getString(R.string.month));
				builder.append(date.get(Calendar.DAY_OF_MONTH) + context.getString(R.string.day));
				builder.append(getPrettyDayString(date, context));
				return builder.toString();
			}
		}
		return DEFAULT_DATE_FORMAT.format(date.getTime());
	}

	private static boolean isYesterday(final Calendar date) {
		Calendar now = Calendar.getInstance();
		return now.getTimeInMillis() / UNIT_DAY - date.getTimeInMillis() / UNIT_DAY == 1;
	}

	private static boolean isInTwoDays(final Calendar date) {
		return System.currentTimeMillis() / UNIT_DAY - date.getTimeInMillis() / UNIT_DAY < 3;
	}

	private static boolean isToday(final Calendar date) {
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.YEAR) == date.get(Calendar.YEAR)
				&& now.get(Calendar.DAY_OF_YEAR) == date.get(Calendar.DAY_OF_YEAR);
	}

	private static boolean isInTenMinutes(final long difference) {
		return difference < UNIT_MINUTE * 10;
	}

	private static boolean isJustNow(final long difference) {
		return difference < UNIT_MINUTE;
	}

	private static String getPrettyDayString(final Calendar date, Context context) {

		return DateFormat.getTimeFormat(context.getApplicationContext()).format(date.getTime());

	}

	/**
	 * 转换字符串时间格式为时间戳
	 * @param string 时间
	 * @param format 格式
	 * @return
	 */
	public static long formatString2Time(String string, String format) {
		try {
			return new SimpleDateFormat(format, Locale.getDefault()).parse(string).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

}
